import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/color.dart';
class ExpandableText extends StatefulWidget {
  final String? text;

  final int? maxLines;

  final TextStyle? style;

  final bool? expand;

  final String? path;

  const ExpandableText(
      this.path,
      {Key? key, this.text, this.maxLines, this.style, this.expand})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _ExpandableTextState(text!, maxLines!, style!, expand!,path!);
  }
}

class _ExpandableTextState extends State<ExpandableText> {
  final String text;

  final int maxLines;

  final TextStyle style;

  bool expand;

  final String? path;

  _ExpandableTextState(this.text, this.maxLines, this.style, this.expand,this.path) {
    if (expand == null) {
      expand = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, size) {
      final span = TextSpan(text: text, style: style);

      final tp = TextPainter(
          text: span, maxLines: maxLines, textDirection: TextDirection.ltr);

      tp.layout(maxWidth: size.maxWidth);

      if (tp.didExceedMaxLines) {
        return InkWell(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: 8.w,top: 2.w),
                  child: Image.asset('images/icon_warning.png',width: 12.w,height: 12.w,)
              ),
              Expanded(
                child: expand
                    ? Text(text, style: style)
                    : Text(text,
                        maxLines: maxLines,
                        overflow: TextOverflow.ellipsis,
                        style: style),
              ),
              GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  setState(() {
                    expand = !expand;
                  });
                },
                child: Container(
                  margin: EdgeInsets.only(top: 2.w,left: 12.w),
                  child: expand ? Image.asset('images/icon_arrow_up.png',width: 12.w,height: 12.w,color: ColorManager.colorEEA151,)
                        : Image.asset('images/icon_arrow_down.png',width: 12.w,height: 12.w,color: ColorManager.colorEEA151,),

                ),
              ),
            ],
          ),
          onTap: (){
            setState(() {
              expand = !expand;
            });
          },
        );
      } else {
        return Row(
          children: [
            Container(
                margin: EdgeInsets.only(right: 8.w,top: 2.w),
                child: Image.asset('images/icon_warning.png',width: 12.w,height: 12.w,)
            ),
            Expanded(
              child: expand
                  ? Text(text, style: style)
                  : Text(text,
                  maxLines: maxLines,
                  overflow: TextOverflow.ellipsis,
                  style: style),
            ),
          ],
        );
      }
    });
  }
}
